﻿using System.Security.Cryptography;
using System.Text;

public static partial class Extensions
{
    /// <summary>
    ///     返回字符串的MD5
    /// </summary>
    /// <param name="this"></param>
    /// <param name="encoding"></param>
    /// <returns></returns>
    public static string ToMd5Hash(this string @this, Encoding encoding = null)
    {
        var md5Hasher = MD5.Create();
        if (encoding == null)
            encoding = Encoding.Default;
        // Convert the input string to a byte array and compute the hash.
        var data = md5Hasher.ComputeHash(encoding.GetBytes(@this));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        var sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (var i = 0; i < data.Length; i++)
            sBuilder.Append(data[i].ToString("x2"));

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }

    
}